﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPN.Common;
using System.IO;

namespace MultiThreading
{
    class WorkerThreadBase_Test
    {
        private static CopyInfo _copyInfo;
        static void Main(string[] args)
        {
            string srcFolder = @"D:\Development";
            //_copyInfo = new CopyInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),  @"c:\pictures", true);
            _copyInfo = new CopyInfo((srcFolder), @"c:\pictures", true);
            
            //Thread simpleThread = new Thread(CopyFilesProc);
            //simpleThread.Name = "CopyFiles";
            //simpleThread.Start();
            //simpleThread.Join();

            //Thread parameterizedThread = new Thread(ParameterizedCopyFilesProc);
            //parameterizedThread.Name = "CopyFiles";
            //parameterizedThread.Start(_copyInfo);
            //parameterizedThread.Join();

            DummyWorker dummyWorker = new DummyWorker();
            dummyWorker.Start();

            CopyFileWorker copyFileWorker = new CopyFileWorker(_copyInfo);
            copyFileWorker.Start();

            //wait for the two threads to finish
            WorkerThreadBase.WaitAll(copyFileWorker, dummyWorker);

        }

        static void CopyFilesProc()
        {
            CopyFiles(_copyInfo);
        }

        static void ParameterizedCopyFilesProc(object state)
        {
            if (state is CopyInfo)
            {
                CopyFiles(state as CopyInfo);
            }
        }

        static void CopyFiles(CopyInfo copyInfo)
        {
            if (!Directory.Exists(copyInfo.Destination))
            {
                Directory.CreateDirectory(copyInfo.Destination);
            }

            Console.WriteLine("CopyFiles from '{0}' to '{1}' {2}...", copyInfo.Source, copyInfo.Destination, copyInfo.Recursive ? "recursive" : "non-recursive");

            foreach (string file in Directory.GetFiles(copyInfo.Source))
            {
                string destination = Path.Combine(copyInfo.Destination,
                    Path.GetFileName(file));
                //File.Copy(file, destination);
                File.Copy(file, destination, true);
            }

            if (copyInfo.Recursive)
            {
                foreach (string directory in Directory.GetDirectories(copyInfo.Source))
                {
                    string destination = Path.Combine(copyInfo.Destination, Path.GetFileName(directory)); //get the directory name for the path
                    CopyFiles(new CopyInfo(directory, destination, copyInfo.Recursive));
                }
            }

            Console.WriteLine("CopyFiles finished.");
        }
    }
}
